#include "pch.h"
#include "UIMain.h"
#include "CheckBox.h"
#include "Button.h"
#include "TableWnd.h"
#include "Menu.h"
#include "List.h"

namespace mini
{
	namespace ui
	{
		bool CUIMain::_insideList(event& e)
		{
			auto pList = dynamic_cast<CList*>(mCheckCtrl);
			if (LEFTUP == e.Messtype || LEFTDBLCLK == e.Messtype)
			{
				if (nullptr != pList->mpChooseItem)
				{
					pList->mpChooseItem->mpWnd->setDrawSelf(false);
				}
				pList->mpChooseItem = pList->_checkListItem(&pList->mListItem, e);

				if (nullptr != pList->mpChooseItem &&
					nullptr != pList->mpChooseItem->mpWnd)
				{
					pList->mpChooseItem->mpWnd->setDrawSelf(true);

					if (CList::tree == pList->mMode)
					{
						if (LEFTUP == e.Messtype)
						{
							for (auto _childrenItem : pList->mpChooseItem->mpWnd->mpChildren)
							{
								if (_childrenItem->getWndType() == ui_checkbox &&
									IsSpace(e.point, _childrenItem))
								{
									auto pCheckBox = dynamic_cast<CCheckBox*>(_childrenItem);
									if (Status_Push == pCheckBox->mState)
									{
										pCheckBox->mState = Status_Normal;
										pList->mpChooseItem->mbDrop = false;
									}
									else
									{
										pCheckBox->mState = Status_Push;
										pList->mpChooseItem->mbDrop = true;
									}
									pList->mpChooseItem->mbUpdate = true;
									break;
								}
							}
						}
						else
						{
							{
								for (auto _childrenItem : pList->mpChooseItem->mpWnd->mpChildren)
								{
									if (_childrenItem->getWndType() == ui_checkbox)
									{
										auto pCheckBox = dynamic_cast<CCheckBox*>(_childrenItem);
										pCheckBox->mState = Status_Normal == pCheckBox->mState ? Status_Push : Status_Normal;
										break;
									}
								}
							}
						}
					}
					if (LEFTUP == e.Messtype)
					{
						pList->selectItem(pList->mpChooseItem);
					}
					else
					{
						pList->doubleClickItem(pList->mpChooseItem);
					}
					pList->mpChooseItem->mbUpdate = false;
					return true;
				}
				else
				{
					pList->selectItem(nullptr);
				}
			}
			else if (MOUSEWHEELDOWN == e.Messtype)
			{
				if (nullptr != pList->mpChooseItem)
				{
					pList->mouseWheel(e.Messtype);
				}
			}
			else if (MOUSEWHEELUP == e.Messtype)
			{
				if (nullptr != pList->mpChooseItem)
				{
					pList->mouseWheel(e.Messtype);
				}
			}
			else if (MOUSEMOVE == e.Messtype)
			{
				if (nullptr != pList->mpChooseItem)
				{
					pList->mfuncMouseMove(e.point.x, e.point.y);
				}
			}
			return false;
		}

		bool CUIMain::_insideCheckBox(event& e)
		{
			if (LEFTUP == e.Messtype)
			{
				auto pCheckBox = dynamic_cast<CCheckBox*>(mCheckCtrl);
				pCheckBox->mState = Status_Normal == pCheckBox->mState ? Status_Push : Status_Normal;
				return true;
			}
			return false;
		}

		bool CUIMain::_insideMenu(event& e)
		{
			auto pMenu = dynamic_cast<CMenu*>(mCheckCtrl);
			for (auto& item : pMenu->mItems)
			{
				if (IsSpace(e.point, (item.second).pWnd))
				{
					if ((item.second).processMsg & e.Messtype)
					{
						if (nullptr != (item.second).callback)
						{
							e.bProcess = (item.second).callback();
							return e.bProcess == TRUE ? true : false;
						}
					}
					for (auto& children : (item.second).childrenItem)
					{
						children.visible = !children.visible;
						children.pWnd->setVisual(children.visible);
					}
				}
			}
			return true;
		}

		bool CUIMain::_insideTable(event& e)
		{
			if (LEFTUP == e.Messtype || LEFTDBLCLK == e.Messtype)
			{
				INT32 idx = 0;
				auto pTable = dynamic_cast<CTableWnd*>(mCheckCtrl);
				for (auto& item : pTable->mSubItemWnds)
				{
					if (IsSpace(e.point, (item).pItemWnd))
					{
						pTable->select(idx);
					}
					else
					{
						++idx;
					}
				}
				return true;
			}
			return false;
		}
	}
}
